<template>
    <el-container>
        <el-header>
            <div class="left-panel">
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-input v-model="search.search_keyword"  class="input-with-search" style="width: 300px" @keyup.enter="upsearch">
                        <template #prepend>
                            <el-select v-model="search.search_field" filterable clearable placeholder="请选择" style="width: 110px">
                                <el-option label="入库单号" value="purchase_ware_code"/>
                                <el-option label="订单编号" value="purchase_process_code"/>
                                <el-option label="加工厂" value="process_factory"/>
                                <el-option label="品名" value="goods_name"/>
                            </el-select>
                        </template>
                    </el-input>
                    <el-select v-model="search.status" placeholder="请选择状态" clearable>
                        <el-option label="未完" :value="0"></el-option>
                        <el-option label="已完成" :value="1"></el-option>
                        <el-option label="全部" :value="-1"></el-option>
                    </el-select>
                    <el-date-picker v-model="search.ware_date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
                    <!-- <el-input v-model="search.purchase_code" placeholder="关联" clearable @keyup.enter="upsearch"></el-input> -->
                    <!-- <el-select v-model="search.apply_id" placeholder="供应商" filterable clearable @change="applyChange(form.apply_id)">
                        <el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
                    </el-select> -->
                    <el-select v-model="search.is_complate" placeholder="请选择状态" filterable clearable>
                        <el-option label="未完" :value="0"></el-option>
                        <el-option label="已完成" :value="1"></el-option>
                        <el-option label="全部" :value="10"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" border :params="query">
                <el-table-column type="selection" width="50" align="center"></el-table-column>
                <el-table-column label="入库类型" prop="purchase_process_code" width="210">
                    <template #default="{row}">
                        <el-tag :type="row.input_type == 1 ? 'info' : row.input_type == 2 ? 'primary' : row.input_type == 3 ? 'success' : 'warning'">{{row.input_type == 1 ? '采购单入库' : row.input_type == 2 ? '染色单入库' : row.input_type == 3 ? '加工单入库' : '其他入库'}}</el-tag>
                        <div class="flex">
                            <el-link :underline="false" type="primary" @click="table_show_process(row)">{{ row.process.purchase_process_code }}</el-link>
                            <el-icon class="icon" :size="20" @click="common.copy(row.process?.purchase_process_code)"><el-icon-Edit /></el-icon>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="入库单号" prop="apply.apply_name" width="220">
                    <template #default="{row}">
                        <div class="flex start">
                            <el-link :underline="false" type="primary" @click="table_show(row)">{{ row.purchase_ware_code }}</el-link>
                            <el-icon class="icon" :size="20" @click="common.copy(row.purchase_ware_code)"><el-icon-Edit /></el-icon>
                            <el-icon class="icon" color="#1e9fff" :size="20" @click="house_bell(row)" v-auth="'machhouse.message'"><el-icon-BellFilled /></el-icon>
                        </div>
                        <div>{{row.ware_date}}({{row.ware_name}})</div>
                    </template>
                </el-table-column>
                <el-table-column label="供应商" prop="date">
                    <template #default="{row}">
                        <div style="margin: 10px 0;">{{row.apply.apply_name}}</div>
                        <div>{{row.apply.linkman}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="染色方">
                    <template #default="{row}">
                        <div style="margin: 10px 0;">{{row.dye_factory?row.dyeFactory.apply_name:''}}</div>
                        <div>{{row.dye_factory?row.dyeFactory.linkman:''}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="加工方">
                    <template #default="{row}">
                        <div style="margin: 10px 0;cursor: pointer;" @click="cellSearch(row.processFactory?.apply_name, 'process_factory')">{{row.process_factory?row.processFactory.apply_name:''}}</div>
                        <div>{{row.process_factory?row.processFactory.linkman:''}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="品名" prop="goods_name" align="center" width="110">
                    <template #default="{row}">
                        <div style="margin: 10px 0;cursor: pointer;" @click="cellSearch(row.goods_name, 'goods_name')">{{row.goods_name}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="数量(匹)" prop="total_num" align="center" width="90"></el-table-column>
                <el-table-column label="单价(元/米)" prop="price" align="center" width="110"></el-table-column>
                <el-table-column label="审核" prop="status" width="100" align="center">
                    <template #default="{row}">
                        <div class="column">
                            <el-button :type="row.status == 0 ? 'warning' : row.status == 1 ? 'primary' : 'danger'" size="small" @click="purchase_check(row)" v-auth="'machorder.check'">{{row.status == 0 ? '待审' : row.status == 1 ? '通过' : '退回'}}</el-button>
                            <el-button type="primary" text size="small" @click="purchase_upload(row)" v-if="row.status == 1" v-auth="'machorder.upload'">查看附件</el-button>
                        </div>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="文件" prop="upload" align="center" width="90">
                    <template #default="{row}">
                        <el-button type="primary" size="small" @click="purchase_upload(row)" v-if="row.status == 1" v-auth="'machhouse.upload'">查看</el-button>
                    </template>
                </el-table-column> -->
                <el-table-column label="备注" prop="remark" align="center" width="100"></el-table-column>
                <el-table-column label="业务员" prop="createstaff.name" align="center" width="100"></el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="280">
                    <template #default="scope">
                        <div class="buttonFlex">
                            <el-button size="small" v-auth="'machhouse.edit'" v-if="scope.row.status == 1">移库</el-button>
                            <el-button size="small" @click="table_weight_note(scope.row)" v-auth="'machhouse.note'">设置码单</el-button>
                            <el-button size="small" @click="table_weight_note_show(scope.row)" v-auth="'machhouse.noteshow'">码单</el-button>
                        </div>
                        <div class="buttonFlex">
                            <el-popconfirm :title="`确定${scope.row.is_complate == 1 ? '未完' : '完成'}吗？`" @confirm="purchase_complate(scope.row)">
                                <template #reference>
                                    <el-button size="small" v-auth="'machhouse.complate'">{{scope.row.is_complate == 1 ? '已完成' : '未完'}}</el-button>
                                </template>
                            </el-popconfirm>
                            <el-button size="small" @click="purchase_print(scope.row)" v-auth="'machhouse.print'">打印</el-button>
                            <el-button type="primary" size="small" @click="table_edit(scope.row)" v-auth="'machhouse.edit'" v-if="scope.row.status != 1">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button type="danger" size="small" v-auth="'machhouse.delete'" v-if="scope.row.status != 1">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </div>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
    <check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false" draggable></check-status>
    <purchase-dialog v-if="dialog.purchase" ref="purchaseDialog" @success="handleSuccess" @closed="dialog.purchase = false"></purchase-dialog>
    <upload-dialog v-if="dialog.upload" ref="uploadDialog" @success="handleSuccess" @closed="dialog.upload = false"></upload-dialog>
    <notice-save v-if="dialog.notice" ref="noticeDialog" @success="handleSuccess" @closed="dialog.notice = false"></notice-save>
    <weight-note v-if="dialog.weight_note" ref="weightNoteDialog" @success="handleSuccess" @closed="dialog.weight_note = false"></weight-note>
    <meter-order v-if="dialog.meter" ref="meterDialog" @success="handleSuccess" @closed="dialog.meter = false"></meter-order>
    
    <el-dialog title="打印" width="500" v-model="dialog.print_select" align-center>
        <el-form label-position="top">
            <el-form-item label="打印关联单">
                <el-select v-model="is_correlation">
                    <el-option label="是" :value="1"></el-option>
                    <el-option label="否" :value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="打印胚布供方">
                <el-select v-model="is_apply">
                    <el-option label="是" :value="1"></el-option>
                    <el-option label="否" :value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="打印价格"> 
                <el-select v-model="is_price">
                    <el-option label="是" :value="1"></el-option>
                    <el-option label="否" :value="0"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button type="primary" @click="open_print">打 印</el-button>
        </template>
    </el-dialog>
    <house-print v-if="dialog.print" ref="printDialog" @success="handleSuccess" @closed="dialog.print = false"></house-print>
</template>

<script>
import CheckStatus from '../../checkStatus.vue'
import uploadDialog from '../../upload.vue'
import saveDialog from './save'
import purchaseDialog from '@/views/business/machining/order/save'
import NoticeSave from '../../noticeSave.vue'
import WeightNote from './weightNote.vue'
import MeterOrder from './meterOrder.vue'

import housePrint from './housePrint.vue'
export default {
    name: '采购计划管理',
    components: {
        saveDialog, CheckStatus, purchaseDialog, uploadDialog, NoticeSave, WeightNote, MeterOrder, housePrint
    },
    data() {
        return {
            dialog: {
                save: false,
                check: false,
                purchase: false,
                upload: false,
                look: false,
                notice: false,
                weight_note: false,
                meter: false,
                print_select: false,
            },
            apiObj: this.$API.business.machining.house.list,
            selection: [],
            search: {
                search_keyword: null,
                search_field: null,
                ware_date: [this.moment().subtract(30, 'days').format('YYYY-MM-DD'), this.moment().format('YYYY-MM-DD')],
                status: null,
                is_complate: 0
            },
            applyList: [],
            purchase_id: "",
            query: {
                input_type: 3
            },
            is_correlation: 1,
            is_price: 1,
            is_apply: 1,
            print_id: "",
        }
    },
    mounted() {
		this.getApply();
    },
    methods: {
        // 搜索
        cellSearch(search_keyword, search_field) {
            this.search.search_keyword = search_keyword
            this.search.search_field = search_field
            this.$refs.table.refresh()
        },
        //添加
        add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        //编辑
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //查看
        table_show(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('show').setData(row)
            })
        },
        //删除
        async table_del(row) {
            var reqData = { id: row.id }
            var res = await this.$API.business.purchase.house.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        async table_del_all(delete_ids) {
            var reqData = { id: delete_ids }
            var res = await this.$API.business.purchase.house.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        //批量删除
        async batch_del() {
            var delete_ids = [];
            this.selection.forEach((item) => {
                delete_ids.push(item.id)
            })

            this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
                type: 'warning'
            }).then(() => {
                this.table_del_all(delete_ids)
            }).catch(() => {

            })
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        //本地更新数据
        handleSuccess(data, mode) {
            if (mode == 'add') {
                this.$refs.table.refresh()
            } else if (mode == 'edit') {
                this.$refs.table.refresh()
            } else if (mode == 'machining_house') {
                this.$refs.table.refresh()
            }
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.search.is_complate = 0
            this.search.ware_date = [this.moment().subtract(30, 'days').format('YYYY-MM-DD'), this.moment().format('YYYY-MM-DD')],
            this.$refs.table.upData(this.search)
        },
        // 供应商列表
		async getApply() {
			var res = await this.$API.basic.apply.all.get();
			this.applyList = res.data
		},
        // 入库审核
        purchase_check(row){
            this.dialog.check = true
            this.$nextTick(() => {
                this.$refs.checkDialog.open('machining_house').setData(row,row.checkstaff?.name)
            })
        },
        // 查看文件
        purchase_upload(row){
            this.dialog.upload = true
            this.$nextTick(() => {
                this.$refs.uploadDialog.open('show').setData(row)
            })
        },
        // 查看加工单
        table_show_process(row){
            let query = {
                id: row.process_id,
                purchase_ware_code: row.process.purchase_process_code
            }
            this.dialog.purchase = true
            this.$nextTick(() => {
                this.$refs.purchaseDialog.open('show').setData(query)
            })
        },
        // 入库提醒
        house_bell(row){
            let data = {
                id: row.id,
                code: row.purchase_ware_code,
                apply_id: row.apply_id
            }
            this.dialog.notice = true
            this.$nextTick(() => {
                this.$refs.noticeDialog.open('edit').setData(data,'加工入库单',10)
            })
        },
        // 设置码单
        table_weight_note(row){
            this.dialog.weight_note = true
            this.$nextTick(() => {
                this.$refs.weightNoteDialog.open('edit').setData(row)
            })
        },
        // 查看码单
        table_weight_note_show(row){
            this.dialog.meter = true
            this.$nextTick(() => {
                this.$refs.meterDialog.open('edit').setData(row)
            })
        },

        // 完成状态切换
        async purchase_complate(row) {
            var reqData = { id: row.id }
            var res = await this.$API.business.machining.house.complate.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("操作成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },


        // 打印选择弹窗
        purchase_print(row){
            this.common.copy(row.purchase_ware_code)
            this.print_id = row.id;
            this.dialog.print_select = true;
        },
        // 确定打印
        open_print(){
            let query = {
                id: this.print_id,
                is_correlation: this.is_correlation,
                is_price: this.is_price,
                is_apply: this.is_apply,
            }
            this.dialog.print_select = false;
            this.dialog.print = true;
            this.$nextTick(() => { 
                this.$refs.printDialog.open('print').setData(query)
            })
            this.is_correlation = 1
            this.is_price = 1
            this.is_apply = 1
        },
    }
}
</script>

<style lang="scss">
.flex {
    display: flex;
    align-items: center;
    justify-content: center;
    .icon{
        cursor: pointer;
        margin-left: 5px;
    }
}
</style>
